package com.yanggu.mapreduce.groupingcomparator;

import lombok.Data;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2020/2/7 16:23
 * @description:
 */
@Data
public class OrderBean implements WritableComparable<OrderBean> {

    private Integer id;

    private Double price;

    @Override
    public int compareTo(OrderBean orderBean) {
        int compare = this.id - orderBean.getId();
        int result;
        if (compare > 0) {
            result = 1;
        } else if (compare < 0) {
            result = -1;
        } else {
            double compare1 = this.price - orderBean.getPrice();
            if (compare1 < 0) {
                result = 1;
            } else if (compare1 > 0) {
                result = -1;
            } else {
                result = 0;
            }
        }
        return result;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeInt(this.id);
        out.writeDouble(this.price);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.id = in.readInt();
        this.price = in.readDouble();
    }

    @Override
    public String toString() {
        return id + "\t" + price;
    }
}
